﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Itfort;
using MySoft.Data;

namespace Users
{
	/// <summary>
	/// 社交帐号绑定服务。
	/// </summary>
	public class UserSnsService : IUserSnsService
	{
		private DbSession dbSession = new DbSession("Users");

		/// <summary>
		/// 社交账号绑定信息是否存在。
		/// </summary>
		/// <param name="snsType">社交账号类型。</param>
		/// <param name="snsId">社交账号编号。</param>
		/// <returns></returns>
		public bool Exists(SnsType snsType, string snsId)
		{
			WhereClip where = _UserSns._.SnsId == snsId && _UserSns._.SnsType == (int)snsType;
			return dbSession.Exists<_UserSns>(where);
		}

		/// <summary>
		/// 获取社交账号绑定信息。
		/// </summary>
		/// <param name="snsType">社交帐号类型。</param>
		/// <param name="snsId">社交账号编号。</param>
		/// <returns></returns>
		public UserSns GetUserSns(SnsType snsType, string snsId)
		{
			WhereClip where = _UserSns._.SnsId == snsId && _UserSns._.SnsType == (int)snsType;
			return dbSession.Single<_UserSns>(where).Convert2<UserSns>();
		}

		/// <summary>
		/// 获取社交账号绑定信息。
		/// </summary>
		/// <param name="userId">用户编号。</param>
		/// <param name="snsType">社交账号类型。</param>
		/// <returns></returns>
		public UserSns GetUserSns(int userId, SnsType snsType)
		{
			WhereClip where = _UserSns._.UserId == userId && _UserSns._.SnsType == (int)snsType;
			return dbSession.Single<_UserSns>(where).Convert2<UserSns>();
		}

		/// <summary>
		/// 获取某个用户绑定的社交账号列表
		/// </summary>
		/// <param name="userId">用户编号</param>
		/// <returns></returns>
		public List<UserSns> GetUserSnsList(int userId)
		{
			WhereClip where = _UserSns._.UserId == userId;
			return dbSession.From<_UserSns>().Where(where).ToList().Convert2<UserSns>();
		}

		/// <summary>
		/// 根据设计账号创建用户
		/// </summary>
		/// <param name="snsType">社交账号类型</param>
		/// <param name="snsId">社交账号编号</param>
		/// <param name="accessToken">访问令牌</param>
		/// <param name="snsNick">社交账号昵称</param>
		/// <returns></returns>
		public UserSns CreateUser(SnsType snsType, string snsId, string accessToken, string snsNick)
		{
			var dbTrans = dbSession.BeginTrans();
			try
			{
				UserService userService = new UserService();
				// 当SNS昵称在已注册用户中不重复时，直接使用，否则以SNS昵称开头随机生成一个
				string nickname = snsNick;
				if (userService.ExistsNickname(snsNick))
				{
					nickname = snsNick + DateTime.Now.ToString("yyyyMMddHHmmss");
				}
				User user = userService.CreateUser(dbTrans, null, null, null, null, 0, 0, nickname);

				_UserSns _userSns = new _UserSns();
				_userSns.UserId = user.Id;
				_userSns.SnsId = snsId;
				_userSns.SnsType = (int)snsType;
				_userSns.CreationTime = user.CreationTime;
				_userSns.AccessToken = accessToken;
				_userSns.SnsNick = snsNick;

				dbTrans.Save(_userSns);
				dbTrans.Commit();
				return this.GetUserSns(snsType, snsId);
			}
			catch
			{
				dbTrans.Rollback();
				throw;
			}
		}

		/// <summary>
		/// 绑定社交账号
		/// </summary>
		/// <param name="userId">用户编号</param>
		/// <param name="snsType">社交账号类型</param>
		/// <param name="snsId">社交账号编号</param>
		/// <param name="accessToken">访问令牌</param>
		/// <param name="snsNick">社交账号昵称</param>
		/// <returns></returns>
		public UserSns BindUser(int userId, SnsType snsType, string snsId, string accessToken, string snsNick)
		{
			_UserSns _userSns = new _UserSns();
			_userSns.UserId = userId;
			_userSns.SnsId = snsId;
			_userSns.SnsType = (int)snsType;
			_userSns.CreationTime = DateTime.Now;
			_userSns.AccessToken = accessToken;
			_userSns.SnsNick = snsNick;

			dbSession.Save(_userSns);
			return this.GetUserSns(userId, snsType);
		}

		/// <summary>
		/// 解除社交账号
		/// </summary>
		/// <param name="userId">用户编号</param>
		/// <param name="snsType">社交账号类型</param>
		public void Unbind(int userId, SnsType snsType)
		{
			dbSession.Delete<_UserSns>(_UserSns._.UserId == userId && _UserSns._.SnsType == (int)snsType);
		}

		/// <summary>
		/// 修改社交账号绑定信息
		/// </summary>
		/// <param name="userSns"></param>
		public void UpdateUserSns(UserSns userSns)
		{
			_UserSns _userSns = userSns.Convert2<_UserSns>();
			_userSns.AttachAll();

			dbSession.Save(_userSns);
		}
	}
}
